package com.yls.except.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * long，date，string类型的时间格式的相互转换
 * 
 * @author xusheng
 * @time 2015-5-14 下午2:38:25
 * @version 1.0
 * @Description: {}
 * 
 */
public class DateStringLong {
	public static long stringToLong(String strTime, String formatType) {
		Date date;
		date = stringToDate(strTime, formatType);
		if (date == null) {
			return 0;
		} else {
			long currentTime = dateToLong(date); // date类型转成long类型
			return currentTime;
		}
	} // String类型转成date类型

	public static Date stringToDate(String strTime, String formatType) {
		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
		Date date = null;
		try {
			date = formatter.parse(strTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static long dateToLong(Date date) {
		return date.getTime();
	}

	public static String dateToString(Date data, String formatType) {
		return new SimpleDateFormat(formatType).format(data);
	}

	public static String longToString(long currentTime, String formatType) {
		Date date = null;
		String strTime = null;
		date = longToDate(currentTime, formatType);
		strTime = dateToString(date, formatType); // date类型转成String
		return strTime;
	}

	// long类型转成Date类型
	public static Date longToDate(long currentTime, String formatType) {
		Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
		String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
		Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
		return date;
	}
	
	/**
	 * 获得当前年-月-日
	 * @return
	 */
	public static Integer IntDate() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DAY_OF_MONTH);
		String date = "";
		date = year + "-";
		if (month < 10) {
			date += "0" + month + "-";
		} else {
			date += month + "-";
		}
		if (day < 10) {
			date += "0" + day;
		} else {
			date += day;
		}
		return (int) (stringToLong(date, "yy-MM-dd") / 1000);
	}
	
	/**
	 * 获得当前时间和几年后的时间
	 * @param howyear 几年
	 * @return 当前时间和几年后的时间
	 */
	public static Map<String, Integer> oneyear(int howyear) {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DAY_OF_MONTH);
		int hh = ca.get(Calendar.HOUR_OF_DAY);
		int mm = ca.get(Calendar.MINUTE);
		int ss = ca.get(Calendar.SECOND);
		int sss = ca.get(Calendar.MILLISECOND);
		;
		String end_date = (year + howyear) + "-";
		String date = "";
		date = year + "-";
		if (month < 10) {
			date += "0" + month + "-";
			end_date += "0" + month + "-";
		} else {
			date += month + "-";
			end_date += month + "-";
		}
		if (day < 10) {
			date += "0" + day;
			end_date += "0" + day;
		} else {
			date += day;
			end_date += day;
		}
		if (hh < 10) {
			date += " 0" + hh;
			end_date += " 0" + hh;
		} else {
			date += " " + hh;
			end_date += " " + hh;
		}
		date += ":";
		end_date += ":";
		if (mm < 10) {
			date += "0" + mm;
			end_date += "0" + mm;
		} else {
			date += mm;
			end_date += mm;
		}
		date += ":";
		end_date += ":";
		if (ss < 10) {
			date += "0" + ss;
			end_date += "0" + ss;
		} else {
			date += ss;
			end_date += ss;
		}
		if (sss < 10) {
			date += " 00" + sss;
			end_date += " 00" + sss;
		} else if (sss < 100) {
			date += " 0" + sss;
			end_date += " 0" + sss;
		} else {
			date += " " + sss;
			end_date += " " + sss;
		}
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("first_date",
				(int) (stringToLong(date, "yyyy-MM-dd hh:mm:ss sss") / 1000));
		map.put("end_date",
				(int) (stringToLong(end_date, "yyyy-MM-dd hh:mm:ss sss") / 1000));
		return map;
	}
	
	public static int getMonthLate(int nowtime, int monthlate) {
		int year = Integer.parseInt(longToString(nowtime * 1000l, "yyyy"));
		int month = Integer.parseInt(longToString(nowtime * 1000l, "MM"));
		int day = Integer.parseInt(longToString(nowtime * 1000l, "dd"));
		monthlate += month;
		int yearlate = monthlate / 12;
		monthlate = monthlate - 12 * yearlate;
		year += yearlate;
		System.out.println(year+"-"+monthlate+"-"+day);
		int time = (int)(stringToLong(year+"-"+monthlate+"-"+day, "yyyy-MM-dd") / 1000);
		return time;
	}

	/**
	 * 获取年龄
	 * @param starttime
	 * @param endtime
	 * @return
	 */
	public static int getage(long starttime, long endtime) {
		int startyear = Integer.parseInt(longToString(starttime, "yyyy"));
		int endyear = Integer.parseInt(longToString(endtime, "yyyy"));
		int startmonth = Integer.parseInt(longToString(starttime, "MM"));
		int endmonth = Integer.parseInt(longToString(endtime, "MM"));
		int startday = Integer.parseInt(longToString(starttime, "dd"));
		int endday = Integer.parseInt(longToString(endtime, "dd"));
		if(startday > endday)
			endmonth --;
		if(startmonth > endmonth)
			endyear --;
		int age = endyear - startyear;
		if(age <= 0)
			age = 0;
		return age;
	}

	/**
	 * 根据两个时间段查询相差时间
	 * @param starttime
	 * @param endtime
	 * @return
	 */
	public static Map<String, Object> getTime(long starttime, long endtime) {
		int startyear = Integer.parseInt(longToString(starttime, "yyyy"));
		int endyear = Integer.parseInt(longToString(endtime, "yyyy"));
		int startmonth = Integer.parseInt(longToString(starttime, "MM"));
		int endmonth = Integer.parseInt(longToString(endtime, "MM"));
		int startday = Integer.parseInt(longToString(starttime, "dd"));
		int endday = Integer.parseInt(longToString(endtime, "dd"));
		int starthour = Integer.parseInt(longToString(starttime, "HH"));
		int endhour = Integer.parseInt(longToString(endtime, "HH"));
		int startmin = Integer.parseInt(longToString(starttime, "mm"));
		int endmin = Integer.parseInt(longToString(endtime, "mm"));
		int startsec = Integer.parseInt(longToString(starttime, "ss"));
		int endsec = Integer.parseInt(longToString(endtime, "ss"));
		if(endsec < startsec) {
			endsec += 60;
			endmin = endmin - 1;
		}
		if(endmin < startmin) {
			endmin += 60;
			endhour -= 1;
		}
		if(endhour < starthour) {
			endhour += 24;
			endday -= 1;
		}
		if(endday < startday) {
			endday = getEndday(endyear, startmonth, endday);
			endmonth -= 1;
		}
		if(endmonth < startmonth) {
			endmonth += 12;
			endyear -= 1;
		}
		if(endyear < startyear) {
			endyear = -1;
		}
		if(endyear == -1)
			return null;
		int sec = endsec - startsec;
		int min = endmin - startmin;
		int hour = endhour - starthour;
		int day = endday - startday;
		int month = endmonth - startmonth;
		int year = endyear - startyear;
		Map<String, Object> map = new  HashMap<String, Object>();
		map.put("sec", sec);
		map.put("min", min);
		map.put("hour", hour);
		map.put("day", day);
		map.put("month", month);
		map.put("year", year);
		return map;
	}

	public static int getEndday(int endyear, int endmonth, int endday) {
		switch (endmonth) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                endday += 31;
                break;
            case 2:
                if(endyear % 4 == 0 && endyear % 100 != 0 || endyear % 400 == 0)
                    endday += 29;
                else
                    endday += 28;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                endday += 30;
				break;
        }
		return endday;
	}

	public static void main(String[] args) {
		int time = 1479091380;
		System.out.println(longToString(time * 1000L, "yyyy-MM-dd HH:mm:ss"));
	}
}
